using System;
using CommunityToolkit.Mvvm.ComponentModel;

namespace GoChat_for_Windows.Models
{
    public partial class User : ObservableObject
    {
        [ObservableProperty]
        private int id;

        [ObservableProperty]
        private string username = string.Empty;

        [ObservableProperty]
        private string nickname = string.Empty;

        [ObservableProperty]
        private string status = "offline";

        [ObservableProperty]
        private string ip = string.Empty;

        [ObservableProperty]
        private int port;

        [ObservableProperty]
        private DateTime lastActiveTime = DateTime.Now;

        [ObservableProperty]
        private DateTime updatedAt = DateTime.Now;

        public bool IsOnline
        {
            get
            {
                // 将数据库的UTC时间转换为本地时间
                var localUpdatedAt = updatedAt.ToLocalTime();
                // 计算时间差
                var timeDiff = DateTime.Now - localUpdatedAt;
                // 如果最后更新时间在2分钟内，认为用户在线
                return timeDiff.TotalMinutes <= 2;
            }
        }

        partial void OnUpdatedAtChanged(DateTime value)
        {
            OnPropertyChanged(nameof(IsOnline));
        }

        public string LastSeen
        {
            get
            {
                return lastActiveTime.ToString("yyyy-MM-dd HH:mm:ss");
            }
            set
            {
                if (DateTime.TryParse(value, out DateTime result))
                {
                    LastActiveTime = result;
                }
            }
        }
    }
} 